package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.bo.SpuBo;
import com.leyou.item.pojo.Brand;
import com.leyou.item.pojo.Sku;
import com.leyou.item.pojo.Spu;
import com.leyou.item.pojo.SpuDetail;
import com.leyou.item.service.GoodsService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-05 17:10
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuBo>> getSpu(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows
    ){
        PageResult<SpuBo> list = goodsService.querySpuPageAll(key,saleable,page,rows);
        if (CollectionUtils.isEmpty(list.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }

    @PostMapping("goods")
    public  ResponseEntity<Void> addGoods(@RequestBody SpuBo spuBo){
        goodsService.insertGoods(spuBo);
        return  ResponseEntity.ok().build();
    }

    @PutMapping("goods")
    public  ResponseEntity<Void> updateGoods(@RequestBody SpuBo spuBo){
        goodsService.updateGoods(spuBo);
        return  ResponseEntity.ok().build();
    }


    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> getSpuDetail(@PathVariable("id") Long id){
        SpuDetail spuDetail = goodsService.selectSpuDetail(id);
        return ResponseEntity.ok(spuDetail);
    }

    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> getListSku(@RequestParam("id") Long id){

        List<Sku> list = goodsService.selectSkuById(id);
        return ResponseEntity.ok(list);
    }

    @GetMapping("spu/deleteGoods/{id}")
    public ResponseEntity<Void> deleteGoods(@PathVariable("id") Long id){
        goodsService.deleteGoodsById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("spu/xaijia/{id}")
    public ResponseEntity<Void> xiajiaGoods(@PathVariable("id") Long id){
        goodsService.xiajiaGoodsById(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("spu/shangjia/{id}")
    public ResponseEntity<Void> shangjiaGoods(@PathVariable("id") Long id){
        goodsService.shangjiaGoodsById(id);
        return ResponseEntity.ok().build();
    }

    //根据spuID查找spu对象
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> getSpuById(@PathVariable("id") Long id){
        Spu spu = goodsService.getSpuById(id);
        return ResponseEntity.ok(spu);
    }

    //根据skuId查询sku
    @PostMapping("sku/{id}")
    public ResponseEntity<Sku> getSkuById(@PathVariable("id")Long id){
       Sku sku = goodsService.getSkuById(id);
       return ResponseEntity.ok(sku);
    }


}
